import 'package:flutter/material.dart';
import 'package:full_display/common/qxp_color.dart';
import 'package:full_display/common/qxp_style.dart';
import 'package:full_display/page/montage/dialog/m_addClock_dialog.dart';
import 'package:full_display/page/montage/dialog/m_sn_code_dialog.dart';
import 'package:full_display/page/montage/montage_provider.dart';
import 'package:full_display/unit/asset_utils/asset_utils.dart';
import 'package:full_display/unit/screen/screen_utility.dart';
import 'package:full_display/unit/widgets/widget_dialog/widget_dialog.dart';
import 'package:provider/provider.dart';

class MPinjieManagerWidget extends StatelessWidget {
  const MPinjieManagerWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [ChangeNotifierProvider(create: (_) => MontageProvider())],
      child: const MPinjieManagerDialog(),
    );
  }
}

class MPinjieManagerDialog extends StatefulWidget {
  const MPinjieManagerDialog({super.key});

  @override
  State<MPinjieManagerDialog> createState() => _MPinjieManagerDialogState();
}

class _MPinjieManagerDialogState extends State<MPinjieManagerDialog> {
  var _montageProvider = MontageProvider();
  int currentItem = 0;
  @override
  Widget build(BuildContext context) {
    _montageProvider = context.watch<MontageProvider>();
    return Container(
      width: 320.0.W,
      height: 470.0.W,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
            //渐变位置
            begin: Alignment.topCenter, //右上
            end: Alignment.bottomCenter, //左下
            //渐变颜色[始点颜色, 结束颜色]
            colors: [ColorPlate.app_blue, ColorPlate.sub_blue]),
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Padding(
            padding: EdgeInsets.fromLTRB(10.0.W, 20.0.H, 10.0.W, 20.0.H),
            child: Column(
              children: [
                Text(
                  "拼接管理",
                  style: StandardTextStyle.textWhite17Style,
                  textAlign: TextAlign.center,
                ),
                Container(
                  height: 20.0.H,
                ),
                btnItem(_montageProvider.pinjieManagerBtn),
                Container(
                  height: 15.0.H,
                ),
                GridView.builder(
                  padding: EdgeInsets.zero,
                  scrollDirection: Axis.vertical,
                  physics: const NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: _montageProvider.pinjieManagertitle.length,
                  itemBuilder: (con, index) {
                    return titleItem(
                        index, _montageProvider.pinjieManagertitle[index]);
                  },
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 5,
                    crossAxisSpacing: 10,
                    mainAxisSpacing: 10,
                    //宽高比
                    childAspectRatio: 1,
                  ),
                ),
              ],
            ),
          ),
          Column(
            children: [
              Container(
                padding: EdgeInsets.only(top: 15.0.W),
                child: Divider(
                  height: 1.0.W,
                  color: ColorPlate.white, // 分割线颜色
                ),
              ),
              Container(
                height: 50.0.W,
                child: Row(
                  children: [
                    Expanded(
                      child: InkWell(
                        onTap: () {
                          // widget.okBtnTap();
                          Navigator.pop(context);
                        },
                        child: Center(
                          child: Text(
                            '确定',
                            style: StandardTextStyle.textWhite16Style,
                            textAlign: TextAlign.center,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget btnItem(List list) {
    List<Widget> listView = [];
    for (var i = 0; i < list.length; i++) {
      Map map = list[i];
      listView.add(
        InkWell(
          onTap: () {
            setState(() {
              currentItem = i;
              _montageProvider.changePinjieManagerState(i);
            });
          },
          child: Container(
            width: 80.0.W,
            height: 50.0.W,
            decoration: BoxDecoration(
              //设置边框
              border: Border.all(color: Colors.white, width: 1),
              //背景颜色
              color: map["isSelect"] == true
                  ? ColorPlate.white
                  : ColorPlate.backgroundOpacity0,
              //设置圆角
              borderRadius: BorderRadius.circular((25.0.W)),
            ),
            child: Center(
              child: Text(
                map["title"],
                style: map["isSelect"] == true
                    ? StandardTextStyle.textBlue17BoldStyle
                    : StandardTextStyle.textWhite17BoldStyle,
                textAlign: TextAlign.center,
              ),
            ),
          ),
        ),
      );
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: listView,
    );
  }

  Widget titleItem(int index, String name) {
    return InkWell(
      onTap: () {
        showDialog(
          barrierDismissible: true,
          context: context,
          builder: (context) {
            return DialogWidget(
              contentWidget: MSNCodeDialogWidget(sncode: (sn) {
                print("==========>${sn}");
              }),
            );
          },
        );
      },
      child: Container(
        width: 50.0.W,
        height: 50.0.W,
        decoration: BoxDecoration(
          //设置边框
          border: Border.all(color: Colors.white, width: 1),
          //背景颜色
          color: ColorPlate.backgroundOpacity0,
          //设置圆角
          borderRadius: BorderRadius.circular((5.0.W)),
        ),
        child: Center(
          child: Text(
            name,
            style: StandardTextStyle.textWhite17Style,
            textAlign: TextAlign.center,
          ),
        ),
      ),
    );
  }
}
